home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Celestin Apprentice 7
/
Apprentice-Release7.iso
/
Environments
/
Small Eiffel 0.4.8
/
lib_rand
/
min_stand.e
< prev
next >
Wrap
Text File
|
1997-04-13
|
2KB
|
100 lines
-- Part of SmallEiffel -- Read DISCLAIMER file -- Copyright (C)
-- Dominique COLNET and Suzanne COLLIN -- colnet@loria.fr
--
class MIN_STAND
--
-- Implements the Minimal Standard generator from Press et. al.
-- Numerical Recipies.
--
inherit GEN_RAND;
creation make, with_seed
feature {NONE}
ia: INTEGER is 16807;
im: INTEGER is 2147483647;
iq: INTEGER is 127773;
ir: INTEGER is 2836;
seed:INTEGER
feature {NONE}
make is
local
i: INTEGER;
seed_init: INTEGER;
do
tmp_string.clear;
tmp_string.extend('0');
Current.to_pointer.append_in(tmp_string);
from
i := tmp_string.count;
until
i = 0
loop
if not tmp_string.item(i).is_digit then
tmp_string.remove(i);
end;
i := i - 1;
end;
seed_init := tmp_string.to_integer;
from
until
seed_init < im
loop
seed_init := seed_init - iq;
end;
with_seed(seed_init);
end;
with_seed(seed_value: INTEGER) is
require
valid_seed: seed_value > 0 and seed_value < im
do
seed:= seed_value;
next;
end;
feature
next is
local
k: INTEGER;
do
k := seed // iq;
seed := ia * (seed -k * iq) - ir * k;
if seed < 0 then
seed := seed + im;
end;
end;
last_real: REAL is
do
Result := (seed/im).to_real;
end;
last_integer(n:INTEGER): INTEGER is
do
Result := seed \\ n+1;
end;
feature {NONE}
tmp_string: STRING is
once
!!Result.make(32);
end;
invariant
good_seed: seed > 0 and seed < im
end -- MIN_STAND